<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getOverviewApi } from '@/api/home'
import { ElMessage } from 'element-plus'
import { 
  Document, 
  Opportunity, 
  User, 
  Calendar, 
  Connection, 
  Histogram,
  ArrowRight
} from '@element-plus/icons-vue'

const router = useRouter()

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0
})

// 加载概览数据
const loadOverviewData = async () => {
  try {
    const result = await getOverviewApi()
    if (result.code === 1) {
      overviewData.value = result.data
    } else {
      ElMessage.error(result.msg || '获取数据失败')
    }
  } catch (error) {
    ElMessage.error('获取数据失败')
  }
}

// 快捷操作跳转
const handleQuickAction = (path) => {
  router.push(path)
}

// 页面加载时获取数据
onMounted(() => {
  loadOverviewData()
})
</script>

<template>
  <div class="home-container">
    <!-- 欢迎标题 -->
    <div class="welcome-section">
      <div class="welcome-title">
        <h1>欢迎您，进入轻客管家系统</h1>
        <p class="welcome-subtitle">轻客管家 - 让客户管理更简单</p>
      </div>
    </div>

    <!-- 概览卡片区域 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-card clue-card">
        <div class="card-header">
          <h2 class="card-title">
            <el-icon class="card-icon"><Connection /></el-icon>
            线索概览
          </h2>
        </div>
        <div class="metrics-grid">
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueTotal }}</div>
            <div class="metric-label">总线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueWaitAllot }}</div>
            <div class="metric-label">待分配线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueWaitFollow }}</div>
            <div class="metric-label">待跟进线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueFollowing }}</div>
            <div class="metric-label">跟进中线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueFalse }}</div>
            <div class="metric-label">伪线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueConvertBusiness }}</div>
            <div class="metric-label">转商机线索数</div>
          </div>
        </div>
      </div>

      <!-- 商机概览 -->
      <div class="overview-card business-card">
        <div class="card-header">
          <h2 class="card-title">
            <el-icon class="card-icon"><Opportunity /></el-icon>
            商机概览
          </h2>
        </div>
        <div class="metrics-grid">
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessTotal }}</div>
            <div class="metric-label">总商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessWaitAllot }}</div>
            <div class="metric-label">待分配商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessWaitFollow }}</div>
            <div class="metric-label">待跟进商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessFollowing }}</div>
            <div class="metric-label">跟进中商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessFalse }}</div>
            <div class="metric-label">回收商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessConvertCustomer }}</div>
            <div class="metric-label">转客户商机数</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions-section">
      <h3 class="section-title">快捷操作</h3>
      <div class="actions-grid">
        <div class="action-card course-card" @click="handleQuickAction('/resource/course')">
          <div class="action-icon">
            <el-icon :size="50"><Document /></el-icon>
          </div>
          <div class="action-content">
            <h3 class="action-title">课程管理</h3>
            <p class="action-subtitle">课程信息维护</p>
          </div>
          <div class="action-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="action-card activity-card" @click="handleQuickAction('/resource/activity')">
          <div class="action-icon">
            <el-icon :size="50"><Calendar /></el-icon>
          </div>
          <div class="action-content">
            <h3 class="action-title">活动管理</h3>
            <p class="action-subtitle">营销活动管理</p>
          </div>
          <div class="action-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="action-card clue-card" @click="handleQuickAction('/clue/list')">
          <div class="action-icon">
            <el-icon :size="50"><Connection /></el-icon>
          </div>
          <div class="action-content">
            <h3 class="action-title">线索管理</h3>
            <p class="action-subtitle">潜在客户追踪</p>
          </div>
          <div class="action-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="action-card business-card" @click="handleQuickAction('/business/list')">
          <div class="action-icon">
            <el-icon :size="50"><Opportunity /></el-icon>
          </div>
          <div class="action-content">
            <h3 class="action-title">商机管理</h3>
            <p class="action-subtitle">销售机会管理</p>
          </div>
          <div class="action-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="action-card customer-card" @click="handleQuickAction('/customer')">
          <div class="action-icon">
            <el-icon :size="50"><User /></el-icon>
          </div>
          <div class="action-content">
            <h3 class="action-title">客户管理</h3>
            <p class="action-subtitle">客户信息维护</p>
          </div>
          <div class="action-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="action-card log-card" @click="handleQuickAction('/system/log')">
          <div class="action-icon">
            <el-icon :size="50"><Histogram /></el-icon>
          </div>
          <div class="action-content">
            <h3 class="action-title">日志统计</h3>
            <p class="action-subtitle">系统操作日志</p>
          </div>
          <div class="action-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
}

.home-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="%23ffffff" opacity="0.1"/><circle cx="75" cy="75" r="1" fill="%23ffffff" opacity="0.1"/><circle cx="50" cy="10" r="0.5" fill="%23ffffff" opacity="0.1"/><circle cx="10" cy="60" r="0.5" fill="%23ffffff" opacity="0.1"/><circle cx="90" cy="40" r="0.5" fill="%23ffffff" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  pointer-events: none;
  z-index: 0;
}

.welcome-section {
  text-align: center;
  margin-bottom: 40px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  padding: 50px 20px;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(79, 172, 254, 0.3);
  color: white;
  position: relative;
  z-index: 1;
  overflow: hidden;
}

.welcome-section::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
  animation: float 6s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.welcome-title {
  margin-bottom: 10px;
}

.welcome-title h1 {
  font-size: 36px;
  font-weight: 700;
  margin: 0;
  color: white;
}

.welcome-subtitle {
  font-size: 18px;
  color: rgba(255, 255, 255, 0.9);
  margin-top: 5px;
}

.overview-section {
  display: flex;
  gap: 25px;
  margin-bottom: 40px;
  position: relative;
  z-index: 1;
}

.overview-card {
  flex: 1;
  background: white;
  border: 1px solid #e4e7ed;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.overview-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.5), transparent);
  transform: translateX(-100%);
  transition: transform 0.6s ease;
}

.overview-card:hover::before {
  transform: translateX(100%);
}

.overview-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
}

.clue-card {
  background: linear-gradient(135deg, #6c5ce7 0%, #4facfe 100%);
}

.business-card {
  background: linear-gradient(135deg, #ff6b6b 0%, #ef5753 100%);
}

.card-header {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
}

.card-title {
  font-size: 24px;
  color: white;
  margin: 0;
  font-weight: 700;
  display: flex;
  align-items: center;
  text-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.card-icon {
  margin-right: 12px;
  color: white;
  opacity: 0.9;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.1));
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 18px;
}

.metric-item {
  text-align: center;
  padding: 18px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 10px;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.metric-item:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: translateY(-3px) scale(1.02);
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.metric-value {
  font-size: 32px;
  font-weight: 800;
  color: white;
  margin-bottom: 8px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.metric-label {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.9);
  line-height: 1.4;
  font-weight: 500;
}

.quick-actions-section {
  background: white;
  border: 1px solid #e4e7ed;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
}

.section-title {
  font-size: 22px;
  color: #333;
  margin-bottom: 25px;
  font-weight: 700;
  text-align: center;
  position: relative;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: linear-gradient(90deg, #4facfe, #00f2fe);
  border-radius: 2px;
}

.actions-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.action-card {
  background: #f8f9fa;
  border: 1px solid #e4e7ed;
  border-radius: 12px;
  padding: 25px 20px;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  position: relative;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  min-height: 160px;
  justify-content: space-between;
  background: linear-gradient(145deg, #ffffff, #f8f9fa);
}

.action-card::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #4facfe, #00f2fe);
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.action-card:hover::after {
  transform: scaleX(1);
}

.action-card:hover {
  border-color: #409eff;
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.25);
  transform: translateY(-5px);
}

.action-card.course-card:hover {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.action-card.activity-card:hover {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
}

.action-card.clue-card:hover {
  background: linear-gradient(135deg, #6c5ce7 0%, #4facfe 100%);
  color: white;
}

.action-card.business-card:hover {
  background: linear-gradient(135deg, #ff6b6b 0%, #ef5753 100%);
  color: white;
}

.action-card.customer-card:hover {
  background: linear-gradient(135deg, #409eff 0%, #50e3c2 100%);
  color: white;
}

.action-card.log-card:hover {
  background: linear-gradient(135deg, #ff9f43 0%, #ee5253 100%);
  color: white;
}

.action-card:hover .action-title,
.action-card:hover .action-subtitle {
  color: white;
}

.action-icon {
  color: #409eff;
  margin-bottom: 15px;
  flex-shrink: 0;
  transition: all 0.3s ease;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 60px;
  height: 60px;
  background: rgba(64, 158, 255, 0.1);
  border-radius: 50%;
  position: relative;
}

.action-icon::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg, #4facfe, #00f2fe);
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.3s ease;
  z-index: -1;
}

.action-card:hover .action-icon::before {
  opacity: 1;
}

.action-card:hover .action-icon {
  color: white;
  transform: scale(1.1);
  background: transparent;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.action-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
}

.action-title {
  font-size: 16px;
  color: #333;
  margin: 0 0 8px 0;
  font-weight: 600;
  line-height: 1.3;
}

.action-subtitle {
  font-size: 12px;
  color: #666;
  margin: 0;
  line-height: 1.4;
}

.action-arrow {
  color: #c0c4cc;
  transition: all 0.3s ease;
  position: absolute;
  top: 15px;
  right: 15px;
  opacity: 0.6;
}

.action-card:hover .action-arrow {
  color: white;
  transform: translateX(3px);
  opacity: 1;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .overview-section {
    flex-direction: column;
    gap: 20px;
  }
  
  .actions-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 18px;
  }
  
  .welcome-title h1 {
    font-size: 32px;
  }
  
  .welcome-subtitle {
    font-size: 16px;
  }
}

@media (max-width: 992px) {
  .actions-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    max-width: 800px;
  }
  
  .action-card {
    min-height: 140px;
    padding: 20px 15px;
  }
  
  .action-icon {
    width: 50px;
    height: 50px;
    margin-bottom: 12px;
  }
  
  .action-title {
    font-size: 15px;
  }
  
  .action-subtitle {
    font-size: 11px;
  }
}

@media (max-width: 768px) {
  .home-container {
    padding: 15px;
  }
  
  .welcome-section {
    padding: 30px 15px;
    margin-bottom: 25px;
  }
  
  .welcome-title h1 {
    font-size: 28px;
  }
  
  .welcome-subtitle {
    font-size: 14px;
  }
  
  .overview-card {
    padding: 20px;
  }
  
  .card-title {
    font-size: 20px;
  }
  
  .metrics-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
  }
  
  .metric-value {
    font-size: 28px;
  }
  
  .metric-label {
    font-size: 12px;
  }
  
  .actions-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
  }
  
  .action-card {
    padding: 18px 12px;
    min-height: 130px;
  }
  
  .action-title {
    font-size: 14px;
  }
  
  .action-subtitle {
    font-size: 10px;
  }
  
  .section-title {
    font-size: 20px;
  }
  
  .quick-actions-section {
    padding: 25px;
  }
}

@media (max-width: 480px) {
  .welcome-title h1 {
    font-size: 24px;
  }
  
  .welcome-subtitle {
    font-size: 12px;
  }
  
  .metrics-grid {
    grid-template-columns: 1fr;
  }
  
  .metric-item {
    padding: 15px;
  }
  
  .metric-value {
    font-size: 24px;
  }
  
  .actions-grid {
    grid-template-columns: 1fr;
    gap: 15px;
    max-width: 100%;
  }
  
  .action-card {
    padding: 20px 15px;
    min-height: 120px;
    flex-direction: row;
    text-align: left;
    justify-content: flex-start;
  }
  
  .action-icon {
    width: 45px;
    height: 45px;
    margin-bottom: 0;
    margin-right: 15px;
  }
  
  .action-content {
    flex: 1;
    text-align: left;
  }
  
  .action-title {
    font-size: 16px;
    margin-bottom: 5px;
  }
  
  .action-subtitle {
    font-size: 12px;
  }
  
  .action-arrow {
    position: static;
    margin-left: auto;
    opacity: 0.8;
  }
}
</style>
